package com.cz;

import cn.hutool.core.collection.CollUtil;
import org.apache.rocketmq.client.QueryResult;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.trace.TraceView;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.topic.TopicValidator;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.tools.admin.api.MessageTrack;
import org.apache.rocketmq.tools.command.message.QueryMsgByUniqueKeySubCommand;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * @Description:
 * @Author: chenzhen
 * @Date: 2023/5/19 14:57
 */
public class MessageStatus {
    public static void main(String[] args) {
        DefaultMQAdminExt adminExt = new DefaultMQAdminExt();
        adminExt.setNamesrvAddr("172.16.5.55:9876"); // 设置 NameServer 地址
        String msgKey = "1802954264646279170";
        try {
            adminExt.start();


/*            QueryResult queryResult = adminExt.queryMessageByUniqKey("czbabl_test", "C0A8EA015A5018B4AAC235C2EDC60000", 32, 0, Long.MAX_VALUE);
            List<MessageExt> msgList = queryResult.getMessageList();
            MessageExt msg = msgList.get(0);
            String storeHost = RemotingHelper.parseSocketAddressAddr(msg.getStoreHost());
            String storeTime = UtilAll.timeMillisToHumanString2(msg.getStoreTimestamp());
            System.out.println(storeHost);
            System.out.println(storeTime);

            System.out.println(queryResult);*/

            QueryResult queryResult = adminExt.queryMessage(TopicValidator.RMQ_SYS_TRACE_TOPIC, msgKey, 64, 0, Long.MAX_VALUE);
            List<MessageExt> messageList = queryResult.getMessageList();
            for (MessageExt message : messageList) {
                System.out.println("***************************************");
                List<TraceView> traceViews = TraceView.decodeFromTraceTransData(msgKey, message);
                traceViews.forEach(traceView->{
                    System.out.println(traceView.getMsgType());
                    System.out.println(traceView.getClientHost());
                    System.out.println(traceView.getCostTime());
                    System.out.println(traceView.getStatus());

                });


            }



        } catch (MQClientException  e) {
            e.getMessage();
            if(e.getResponseCode() == 208){
                QueryResult queryResult1 = null;
                try {
                    queryResult1 = adminExt.queryMessageByUniqKey("h10demoK8s_zgtqspbtg", msgKey, 32, 0, Long.MAX_VALUE);
                } catch (MQClientException ex) {
                    e.printStackTrace();
                } catch (InterruptedException ex) {
                   e.printStackTrace();
                }
                if(CollUtil.isNotEmpty(queryResult1.getMessageList())){
                        queryResult1.getMessageList().forEach(messageExt -> {
                            List<MessageTrack> messageTrackList = null;
                            try {
                                System.out.println(messageExt.toString());
                                messageTrackList = adminExt.messageTrackDetail(messageExt);
                                if(CollUtil.isNotEmpty(messageTrackList)){
                                    MessageTrack messageTrack =  messageTrackList.get(0);
                                    System.out.println(messageTrack.toString());
                                }
                            } catch (RemotingException ex) {
                                ex.printStackTrace();
                            } catch (MQClientException ex) {
                                ex.printStackTrace();
                            } catch (InterruptedException ex) {
                                ex.printStackTrace();
                            } catch (MQBrokerException ex) {
                                ex.printStackTrace();
                            }

                        });
                    }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            adminExt.shutdown();
        }
    }
}
